<%@page import="java.security.MessageDigest"%>
<%@page import="java.security.NoSuchAlgorithmException"%>
<%@page import="java.io.UnsupportedEncodingException"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class=""><head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <meta charset="utf-8">
        <title>NutriWeb - Praticando alimentação saudável</title>

        <meta class="viewport" name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">

        <!-- Favicon -->
        <link rel="shortcut icon" href="a_data/icone_nutriweb.ico">
        <!-- Font -->
        <link rel="stylesheet" href="arquivos_arquivos/css.css">
        <!-- Plagins CSS -->
        <link rel="stylesheet" href="arquivos_arquivos/buttons.css">
        <link rel="stylesheet" href="arquivos_arquivos/social-icons.css">
        <link rel="stylesheet" href="arquivos_arquivos/font-awesome.css">
        <link rel="stylesheet" href="arquivos_arquivos/bootstrap.css">
        <link rel="stylesheet" href="arquivos_arquivos/jslider.css">
        <link rel="stylesheet" href="arquivos_arquivos/settings.css">
        <link rel="stylesheet" href="arquivos_arquivos/jquery.css">
        <link rel="stylesheet" href="arquivos_arquivos/animate.css">
        <link rel="stylesheet" href="arquivos_arquivos/video-js.css">
        <link rel="stylesheet" href="arquivos_arquivos/morris.css">
        <link rel="stylesheet" href="arquivos_arquivos/royalslider.css">
        <link rel="stylesheet" href="arquivos_arquivos/rs-minimal-white.css">
        <link rel="stylesheet" href="arquivos_arquivos/layerslider.css">
        <link rel="stylesheet" href="arquivos_arquivos/ladda.css">
        <link rel="stylesheet" href="arquivos_arquivos/datepicker.css">
        <link rel="stylesheet" href="arquivos_arquivos/jquery_002.css">
        <!-- Theme CSS -->
        <link rel="stylesheet" href="a_data/style.css">
        <!-- Responsive CSS -->
        <link rel="stylesheet" href="a_data/responsive.css">
        <!-- Custom CSS -->
        <link rel="stylesheet" href="a_data/pages.css">
        <link rel="stylesheet" href="a_data/home-pages-customizer.css">
        <!-- IE Styles-->
        <link rel="stylesheet" href="a_data/ie.css">
        <!--[if lt IE 9]>
          <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
          <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
              <link rel='stylesheet' href="css/ie/ie8.css">
        <![endif]-->
        <style type="text/css">
            .jqstooltip { position: absolute;left: 0px;top: 0px;visibility: hidden;background: rgb(0, 0, 0) transparent;background-color: rgba(0,0,0,0.6);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000);-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000)";color: white;font: 10px arial, san serif;text-align: left;white-space: nowrap;padding: 5px;border: 1px solid white;z-index: 10000;}.jqsfield { color: white;font: 10px arial, san serif;text-align: left;}</style>
        <style type="text/css">.fancybox-margin{margin-right:19px;}</style>
        <script style="" src="arquivos_arquivos/commonutilstats.js" charset="UTF-8" type="text/javascript"></script>
    </head>
    <body class="fixed-header loaded fixed"> 
        <div class="page-box">
            <div class="page-box-content">
<%@include file="/templates/header.jsp"%>   
                <div class="breadcrumb-box">
                    <div class="container">
                        <ul class="breadcrumb">
                            <li><a href="inicio.jsp">Início</a> </li>
                            <li class="active">Perfil</li>
                        </ul>	
                    </div>
                </div>
<%@include file="/templates/welcome.jsp"%>
                <div class="clearfix"></div>
                <section id="main">
                    <header class="page-header">
                        <div class="container">
                            <h1 class="title">Perfil</h1>
                        </div>	
                    </header>

                    <div class="container">
                        <div class="panel-group" id="accordion3">

                            <div class="col-sm-6 col-md-6 bottom-padding">

                                <form action="" novalidate="novalidate" class="form-box register-form form-validator bv-form" method="post">
                                    <input type="hidden" name="update"
                                           <% if (session.getAttribute("nm_email") != null) { %>
                                           value="yes"
                                           <% } else { %>
                                           value="no"
                                           <%}%>
                                           >

                                    <h3 class="title">Dados Pessoais</h3>
                                    <p></p>

                                    <div class="form-group has-feedback">
                                        <label>Nome: <span class="required">*</span></label>
                                        <input name="txtNome" type="text"
                                               <% if (session.getAttribute("nm_usuario") != null) { %>
                                               value="${sessionScope.nm_usuario}"
                                               <% } else { %>
                                               value=""
                                               <%}%>
                                               data-bv-field="fullName" class="form-control" 
                                               data-bv-trigger="keyup" required="" 
                                               data-bv-notempty-message="Preencha seu nome." maxlength="50"><i data-bv-icon-for="fullName" 
                                               class="form-control-feedback" style="display: none;"></i>
                                        <small data-bv-result="NOT_VALIDATED" data-bv-for="fullName" 
                                               data-bv-validator="notEmpty" class="help-block" style="display: none;">Preencha seu nome.</small></div>


                                    <div class="form-group has-feedback">
                                        <label>Email: <span class="required">*</span></label>
                                        <input name="txtEmail" type="email"
                                               <% if (session.getAttribute("nm_email") != null) { %>
                                               value="${sessionScope.nm_email}"
                                               <% } else { %>
                                               value=""
                                               <%}%>
                                               data-bv-field="email" class="form-control" data-bv-emailaddress-message="Digite um e-mail válido." data-bv-notempty-message="Preencha seu email." required="required" maxlength="50">
                                        <i data-bv-icon-for="email" class="form-control-feedback" style="display: none;"></i>
                                        <small data-bv-result="NOT_VALIDATED" data-bv-for="email" data-bv-validator="emailAddress" class="help-block" style="display: none;">Digite um e-mail válido.</small>
                                        <small data-bv-result="NOT_VALIDATED" data-bv-for="email" data-bv-validator="notEmpty" class="help-block" style="display: none;">Preencha seu e-mail.</small></div>

                                    <div class="detail-box">
                                        <label>Sexo:</label>
                                        <label class="radio">
                                            <input type="radio" name="rdSexo" value="M"
                                                   <% if (session.getAttribute("ic_sexo") != null) {
                                                    if (session.getAttribute("ic_sexo").equals("M")) {%>
                                                   checked
                                                   <%}%>
                                                   <%}%>
                                                   ></input>M</label>
                                        <label class="radio">
                                            <input type="radio" name="rdSexo" value="F"
                                                   <% if (session.getAttribute("ic_sexo") != null) {
                                                    if (session.getAttribute("ic_sexo").equals("F")) {%>
                                                   checked
                                                   <%}%>
                                                   <%} //session.removeAttribute("nm_usuario");
                                                       //session.removeAttribute("nm_email");
                                                       //session.removeAttribute("ic_sexo");
                                                   %>
                                                   ></input>F</label>
                                    </div>

                                        <div class="form-group has-feedback">
                                            <label>Senha: <span class="required">*</span></label>
                                            <label style="color:red"><%if (session.getAttribute("senha_incorreta") != null) {out.print(session.getAttribute("senha_incorreta").toString());session.removeAttribute("senha_incorreta");}%></label>
                                            <input  name="txtSenha" type="password" value="" data-bv-field="pass" class="form-control" 
                                                    data-bv-field="pass" data-bv-trigger="keyup" data-bv-notempty-message="Digite sua senha." required="required" maxlength="16">
                                            <i data-bv-icon-for="pass" class="form-control-feedback" style="display: none;"></i>
                                            <small data-bv-result="NOT_VALIDATED" data-bv-for="pass" data-bv-validator="notEmpty" class="help-block" style="display: none;">Digite sua senha.</small>
                                        </div>

                                        <label>Nova senha: </label>
                                        <input name="txtConfirmSenha" type="password" value="" class="form-control" maxlength="16">

                                    <div class="buttons-box clearfix">
                                        <button class="btn btn-default" type="submit">
                                        <% if (session.getAttribute("nm_email") != null) {%>Alterar<%} else {%>Cadastrar<%}%>
                                        </button>
                                        <button class="btn btn-default" type="reset">Limpar</button>
                                        <% if (session.getAttribute("nm_email") != null) {%> 
                                        <%}%>

                                        <span class="required"><b>*</b> Campos Obrigatórios.</span>
                                    </div>
                                </form><!-- .form-box -->

                                <%
                                    if (request.getParameter("txtNome") != null
                                            && request.getParameter("txtEmail") != null
                                            && request.getParameter("rdSexo") != null
                                            && request.getParameter("txtSenha") != null) {

                                        String name = request.getParameter("txtNome");
                                        String email = request.getParameter("txtEmail");
                                        String sex = request.getParameter("rdSexo");
                                        String pw = request.getParameter("txtSenha");

                                        String digest = null;
                                        try {
                                            MessageDigest md = MessageDigest.getInstance("MD5");
                                            byte[] hash = md.digest(pw.getBytes("UTF-8"));
                                            StringBuilder sb = new StringBuilder(2 * hash.length);
                                            for (byte b : hash) {
                                                sb.append(String.format("%02x", b & 0xff));
                                            }
                                            digest = sb.toString();
                                        } catch (UnsupportedEncodingException ex) {

                                        } catch (NoSuchAlgorithmException ex) {

                                        }

                                        if (request.getParameter("update") != null && request.getParameter("update").toString().equals("yes")) {

                                            SQL = "SELECT cd_senha FROM \"Operacoes\".\"USUARIO\" WHERE cd_usuario = " + Long.parseLong(session.getAttribute("cd_usuario").toString()) + " ";
                                            try {
                                                for (Object[] reg : Conexao.getQuery(SQL)) {
                                                    if (digest.equals(reg[0].toString())) {
                                                        String nm_email = session.getAttribute("nm_email").toString();

                                                        Object[] params = null;

                                                        SQL = "UPDATE \"Operacoes\".\"USUARIO\" SET nm_usuario= ?, ic_sexo= ?, nm_email= ? ";
                                                        if (request.getParameter("txtConfirmSenha") != null && !request.getParameter("txtConfirmSenha").isEmpty()) {
                                                            String newpw = request.getParameter("txtConfirmSenha");

                                                            String digestNew = null;
                                                            try {
                                                                MessageDigest md = MessageDigest.getInstance("MD5");
                                                                byte[] hash = md.digest(newpw.getBytes("UTF-8"));
                                                                StringBuilder sb = new StringBuilder(2 * hash.length);
                                                                for (byte b : hash) {
                                                                    sb.append(String.format("%02x", b & 0xff));
                                                                }
                                                                digestNew = sb.toString();
                                                            } catch (UnsupportedEncodingException ex) {

                                                            } catch (NoSuchAlgorithmException ex) {

                                                            }

                                                            params = new Object[]{name, sex, email, digestNew, nm_email};

                                                            SQL += ", cd_senha= ? ";
                                                        } else {
                                                            params = new Object[]{name, sex, email, nm_email};
                                                        }
                                                        SQL += " WHERE nm_email = ? ";
                                                        try {
                                                            Conexao.executeStatement(SQL, params);
                                                            session.setAttribute("nm_email", email);
                                                        } catch (Exception e) {
                                                            out.println("Atenção, erro ao atualizar " + e.getLocalizedMessage());
                                                        }
                                                        response.sendRedirect(request.getContextPath() + "/login.jsp");
                                                        return;
                                                    } else {
                                                        session.setAttribute("senha_incorreta", " Atenção, senha incorreta!");
                                                        response.sendRedirect(request.getContextPath() + "/perfil.jsp");
                                                        return;
                                                    }
                                                }
                                            } catch (Exception e) {
                                                out.println("Atenção, " + e.getLocalizedMessage());
                                            }

                                        } else {

                                            Object[] params = params = new Object[]{name, digest, email, sex};

                                            SQL = "INSERT INTO \"Operacoes\".\"USUARIO\" (nm_usuario, cd_senha, nm_email, ic_sexo) "
                                                    + "VALUES (?,?,?,?)";
                                            try {
                                                Conexao.executeStatement(SQL, params);
                                                session.setAttribute("nm_email", email);
                                                response.sendRedirect(request.getContextPath() + "/login.jsp");
                                                return;
                                            } catch (Exception e) {
                                                out.println("Atenção, erro ao inserir " + e.getLocalizedMessage());
                                            }
                                        }
                                    }
                                %>

                            </div>

                        </div>


                    </div>
                </section><!-- #main -->

            </div><!-- .page-box-content -->
        </div><!-- .page-box -->
<%@include file="/templates/footer.jsp"%> 
        <div class="clearfix"></div>

        <script async="" src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/www-widgetapi.js" id="www-widgetapi-script"></script><script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/iframe_api"></script><script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_015.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/bootstrap.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jshashtable-2.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_018.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/tmpl.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_012.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/draggable-0.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_021.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_016.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_022.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_009.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_019.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_011.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_007.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_008.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_005.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/isotope.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_006.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_017.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_020.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_003.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_013.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/SmoothScroll.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/country.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/spin.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/ladda.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/masonry.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/morris.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/raphael.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/video.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/pixastic.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/livicons-1.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/greensock.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/layerslider_002.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/layerslider.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_014.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_004.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/bootstrapValidator.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/bootstrap-datepicker.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_010.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jplayer.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/jquery_002.js"></script>
        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/main.js"></script>


        <script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/js" type="text/javascript"></script><script src="Accordions%20&amp;%20Toggles%20_%20Progressive%20%E2%80%94%20Responsive%20Multipurpose%20HTML%20Template_arquivos/main_002.js"></script>



    </body>

</html>